package com.kaibes.platform.file.client.api;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kaibes.core.base.annotation.Description;
import com.kaibes.platform.file.client.db.FileMap;
import com.kaibes.platform.file.client.db.FileMapService;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.api.ResponseApi;
import com.kaibes.web.response.ResponseFactory;

@RestController
@RequestMapping("/files/maps")
public class FileMapApi extends ResponseApi<FileMap, FileMapService> {
	
	@ApiLink("创建映射")
    @PostMapping
    public ResponseEntity<List<Number>> post(@RequestAttribute Integer userId, @RequestBody FileMap fileMap) {
        fileMap.setUserId(userId);
        if (fileMap.getTag() == null) {
        	fileMap.setTag("");
        }
        return super.post(fileMap, "创建文件映射成功", "创建文件映射失败");
    }

    @DeleteMapping("/{id:\\d+}")
    @ApiLink("删除文件，根据id")
    public ResponseEntity<Object> deleteByIdQuery(@PathVariable @Description("映射ID") Integer id) {
        return super.deleteById(id, "删除文件映射成功", "删除文件映射失败");
    }
    
    @DeleteMapping
    @ApiLink("删除文件，根据id数组")
    public ResponseEntity<Object> deleteByIdsQuery(@RequestBody @Description("ID数组") List<Integer> ids) {
        return super.deleteByIds(ids, "删除文件映射成功", "删除文件映射失败");
    }
    
    //======================
    
    @ApiLink("删除文件，根据用id数组，只能删除自己的")
    @DeleteMapping("/self")
    public ResponseEntity<Object> deleteByUser(@RequestAttribute Integer userId, @RequestBody @Description("ID数组") List<Long> ids) {
        return ResponseFactory.delete(service.removeByUserIdAndIds(userId, ids), "删除文件映射成功", "删除文件映射失败");
    }
    
    @ApiLink("删除文件，根据id，只能删除自己的")
    @DeleteMapping("/self/{id:\\d+}")
    public ResponseEntity<Object> delete(@RequestAttribute Integer userId, @PathVariable @Description("用户ID") Integer id) {
        return ResponseFactory.delete(service.removeByUserIdAndId(userId, id), "删除文件映射成功", "删除文件映射失败");
    }
    
    @ApiLink("删除文件，根据id，只能删除自己的")
    @DeleteMapping("/self/uuid/{uuid}")
    public ResponseEntity<Object> deleteByUuid(@RequestAttribute Integer userId, @PathVariable @Description("用户ID") String uuid) {
        return ResponseFactory.delete(service.removeByUserIdAndUuid(userId, uuid), "删除文件映射成功", "删除文件映射失败");
    }
    
}
